package com.bjkcst.baselib.protocolparse;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.bjkcst.baselib.protocolparse.annotation.BeanFieldAnnotation;
import com.bjkcst.baselib.protocolparse.annotation.BeanFieldBitAnnotation;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author: glc.
 * Date: 2022/5/16  18:30
 * Description:
 */
public class OrderedFieldUtils {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static List<Field> getOrderedField(Field[] fields){
        // 用来存放所有的属性域
        List<Field> fieldList = new ArrayList<>();
        // 过滤带有注解的Field
        for(Field f:fields){
            if(f.getAnnotation(BeanFieldAnnotation.class)!=null){
                fieldList.add(f);
            }
        }
        fieldList.sort(Comparator.comparingInt(
                m -> m.getAnnotation(BeanFieldAnnotation.class).order()
        ));
        return fieldList;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    public static List<Field> getBitField(Field[] fields){
        // 用来存放所有的属性域
        List<Field> fieldList = new ArrayList<>();
        // 过滤带有注解的Field
        for(Field f:fields){
            if(f.getAnnotation(BeanFieldBitAnnotation.class)!=null){
                fieldList.add(f);
            }
        }
        fieldList.sort(Comparator.comparingInt(
                m -> m.getAnnotation(BeanFieldBitAnnotation.class).order()
        ));
        return fieldList;
    }
}
